package com.chivorn.picker.adapter;

import com.chivorn.picker.model.MonthModel;

/**
 * Created by Hum Vorn on 1/3/2018.
 */

public class MonthAdapter implements WheelAdapter {
    private int max = 12;
    private int min = 1;
    private boolean isNumber = false;

    public MonthAdapter(int max, int min,boolean isNumber) {
        this.max = max;
        this.min = min;
        this.isNumber = isNumber;
        if (max < min)
            this.max = this.min;
//        Log.e("ad", "--ad-" + max + ";" + min);
    }

    @Override
    public int getItemsCount() {
        //10-5
        return max - min + 1;
    }

    @Override

    public Object getItem(int i) {
        if (i >= 0 && i < getItemsCount()) {
            //10-5 -> po =0 -> get index=4 ; po =5 -> get index=
            int p = min + i - 1;
            if (p < 0)
                p = 0;
            if (p >= MonthModel.getMonth(isNumber).size())
                p = MonthModel.getMonth(isNumber).size() - 1;
            return MonthModel.getMonth(isNumber).get(p);
        }
        return "";
    }

    @Override
    public int indexOf(Object obj) {
        //设置 当前位置
        int po = MonthModel.getMonth(isNumber).indexOf((String) obj);
        //  obj=6月 ,p= 5 min=2 -> index = 4
        return po - min + 1;
//        for (int p = 0; p < MonthModel.getMonth().size(); p++) {
//            if (obj.equals(MonthModel.getMonth().get(p))) {
//                int index = p - min;
//                if (index < 0)
//                    index = 0;
//                return index;
//            }
//        }
//        return -1;
    }

    /**
     * 获取真实的月份
     *
     * @param i index
     * @return month num
     */
    public int getItemMonth(int i) {
        if (i >= 0 && i < getItemsCount()) {
            //10-5 -> po =0 -> get index=4 ; po =5 -> get index=
            int p = min + i - 1;
            if (p < 0)
                p = 0;
            if (p >= MonthModel.getMonth(isNumber).size())
                p = MonthModel.getMonth(isNumber).size() - 1;
            return p + 1;
        }
        return 1;
    }

    /**
     * 获取某月份的在当前Adapter的 Index
     *
     * @return index
     */
    public int getIndexOfMonth(int month) {
        // mouth = 6 ; min = 5 -> index= 1
        if (month < min)
            return 0;
        if (month > max)
            return getItemsCount() - 1;
        return month - min;
    }
}
